<?php

require_once APPPATH."/controllers/BasePriv.php";
require APPPATH."/libraries/FFmpeg.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Photo_frame extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_add_activity_model", "table");
        $this->template_photo = "template_photo";
    }

    /**
     * 视频模板首页
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;

        //查询数据（原始素材）
        $res=$this->table->photo_all($arr);
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $this->layout("no_edit/photo_frame/index",$data);
    }

    /**
     * 新增封面模板
     */
    public function add_photo_frame(){
        $this->layout("no_edit/photo_frame/add_photo_frame");
    }

    /**
     *保存封面模板
     */
    public function save_add_photo_frame(){
        if(empty($_POST['photo_frame_title'])){
            response(-1,"请填写视频标题");
        }
        if(empty($_POST['xiaoguo_url'])){
            response(-1,"请选择效果图片");
        }
        if(empty($_POST['moban_url'])){
            response(-1,"请选择模板图片");
        }

        $data['photo_frame_title']=$_POST['photo_frame_title'];
        $data['xiaoguo_url']=$_POST['xiaoguo_url'];
        $data['moban_url']=$_POST['moban_url'];
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->add($this->template_photo,$data);
        if($res){
//            response(0,"添加成功",base_url("no_edit/video/edit_add_video?id={$res}"));
            response(0,"添加成功",base_url("no_edit/photo_frame/index"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改页面加载
     */
    public function edit_add_photo_frame(){
        $id=$_GET['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->one($this->template_photo,['id'=>$id]);
        $data['data']=$res;
        $this->layout("no_edit/photo_frame/edit_add_photo_frame",$data);
    }

    /**
     * 保存修改
     */
    public function save_edit_photo_frame(){
        if(empty($_POST['id'])){
            response(-1,"缺少参数");
        }
        if(empty($_POST['photo_frame_title'])){
            response(-1,"请填写视频标题");
        }
        if(empty($_POST['xiaoguo_url'])){
            response(-1,"请选择效果图片");
        }
        if(empty($_POST['moban_url'])){
            response(-1,"请选择模板图片");
        }

        $data['photo_frame_title']=$_POST['photo_frame_title'];
        $data['xiaoguo_url']=$_POST['xiaoguo_url'];
        $data['moban_url']=$_POST['moban_url'];
        $data['update_time']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->update($this->template_photo,$data,['id'=>$_POST['id']]);
        if($res){
//            response(0,"添加成功",base_url("no_edit/video/edit_add_video?id={$res}"));
            response(0,"修改成功",base_url("no_edit/photo_frame/index"));
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 停用
     */
    public function photo_frame_stop(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['photo_online']=1;
        $res=$this->table->update($this->template_photo,$data,['id'=>$id]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }

    /**
     * 启用
     */
    public function photo_frame_up(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['photo_online']=2;
        $res=$this->table->update($this->template_photo,$data,['id'=>$id]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 删除
     */
    public function photo_frame_del(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->photo_frame_del(['id'=>$id]);
        if($res){
            response(0,"删除成功");
        }else{
            response(-1,"删除失败");
        }
    }
}